package com.jingdianjichi.oss.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.jingdianjichi.oss.service.FileService;
import com.jingdianjichi.oss.util.MinioUtil;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * FileController 类
 *
 * @author 熊苏鹏1
 * @date 2025/2/27 21:02
 * @description
 */
@RestController
public class FileController {
    @Value("${storage.service.type}")
    private String storageType;
    @Resource
    private FileService fileService;
    @Resource
    MinioUtil minioUtil;

    @GetMapping("/testGetAllBuckets")
    public List<String> testGetAllBuckets() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return fileService.getAllBucket();
    }
    @GetMapping("/testNacos")
    public String testNacos() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return storageType;
    }
    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file, String bucket, String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        fileService.uploadFile(file, bucket, objectName);
        return fileService.getUrl(bucket, objectName);
    }
}
